//
//  XLNavigationViewController.swift
//  LaowuquanS
//
//  Created by 王小林 on 2017/8/30.
//  Copyright © 2017年 王小林. All rights reserved.
//

import UIKit

class XLNavigationViewController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
         UINavigationBar.appearance().barTintColor = kXLNavBarColor
        // 设置naviBar背景图片
//        UINavigationBar.appearance().setBackgroundImage(UIImage.init(named: "navigationbarBackgroundWhite"), for: UIBarMetrics.default)
        // 设置title的字体
        UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 18),NSAttributedString.Key.foregroundColor:UIColor.white]
        self.interactivePopGestureRecognizer?.delegate = nil
    }

    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        //print(self.viewControllers.count);
        if self.viewControllers.count < 1 {
//            viewController.navigationItem.rightBarButtonItem = setRightButton()
            
        } else {
            viewController.hidesBottomBarWhenPushed = true
            viewController.navigationItem.leftBarButtonItem = setBackBarButtonItem()
        }
        super.pushViewController(viewController, animated: true)
    }
    // MARK: - private method
    func setBackBarButtonItem() -> UIBarButtonItem {
        
        let backButton = UIButton.init(type: .custom)
        backButton.setImage(UIImage(named: "conventional_icon_return"), for: .normal)
        backButton.sizeToFit()
        backButton.contentEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        backButton.addTarget(self, action: #selector(XLNavigationViewController.backClick), for: .touchUpInside)
        return UIBarButtonItem.init(customView: backButton)
    }
    
    /// 设置导航栏右边按钮
    func setRightButton() -> UIBarButtonItem {
        
        let searchItem = UIButton.init(type: .custom)
//        searchItem.setImage(UIImage(named: "searchbutton_nor"), for: .normal)
        searchItem.setTitle("搜索", for: .normal)
        searchItem.sizeToFit()
        searchItem.frame.size = CGSize(width: 30, height: 30)
        searchItem.contentHorizontalAlignment = .right
        searchItem.addTarget(self, action: #selector(XLNavigationViewController.searchClick), for: .touchUpInside)
        return UIBarButtonItem.init(customView: searchItem)
    }
    
    /// 点击右边的搜索
    @objc func searchClick() {
        
        print("点击了搜索");
//        let searchvc = SearchVC()
//        self.pushViewController(searchvc, animated: true)
    }
    
    /// 返回
    @objc func backClick() {
        self.popViewController(animated: true)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
